package com.sensetime.bi.senselink.server.entity;

import com.sensetime.bi.senselink.server.constant.Constant;
import com.sensetime.bi.senselink.server.util.StringUtils;
import io.netty.handler.codec.rtsp.RtspHeaderNames;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * Rtsp 响应报文实体类 《充血模型》
 * RTSP/1.0 200 OK
 * CSeq: 3
 */
@Slf4j
@Setter
@Getter
public class RtspResponsetMsg {
    String[] lines;
    String status;
    String protocol;
    String CSEQ;
    int contentLength;

    // authorize 相关
    String authType;
    HashMap<String, String> authPairs = new HashMap<>(5);

    public RtspResponsetMsg(String response) {
        lines = StringUtils.getRawLines(response);

        int idx = 0;
        for (String line : lines) {
            if (idx++ == 0) {
                parseFirstLine(line);
            } else {
                String str = line.trim().toUpperCase();
                if (str.startsWith("CSEQ")) {
                    String[] tks = StringUtils.getTokens(line, ":");
                    CSEQ = tks[1].trim();
                } else if (str.startsWith(RtspHeaderNames.WWW_AUTHENTICATE.toUpperCase().toString())) {
                    // WWW-Authenticate: Digest realm="Login to 5A03417PAK8C4B3", nonce="adc823765fae16d58a43699f23016802"
                    String[] authContents = StringUtils.getTokens(line, ":");
                    String authctn = authContents[1].trim();
                    String kvPairStr = "";
                    if (authctn.toLowerCase().startsWith("digest")) {
                        this.authType = "Digest";
                        kvPairStr = authctn.substring(6);
                    } else if (authctn.toLowerCase().startsWith("basic")) {
                        this.authType = "Basic";
                        kvPairStr = authctn.substring(5);
                    } else {
                        continue;
                    }
                    String[] kvs = StringUtils.getTokens(kvPairStr, ",");
                    for (String onekv : kvs) {
                        String[] pair = StringUtils.getTokens(onekv, "=");
                        if (pair.length > 1) {
                            addAuthPair(pair[0].trim(), pair[1].trim().replaceAll("\"", ""));
                        }
                    }
                } else if (str.startsWith(RtspHeaderNames.CONTENT_LENGTH.toUpperCase().toString())) {
                    // Content-Length: 255
                    String[] tks = StringUtils.getTokens(line, ":");
                    contentLength = Integer.parseInt(tks[1].trim());
                }

            }
        }
    }

    private void parseFirstLine(String line) {
        String[] tokens = StringUtils.getTokens(line, Constant.RTSPTOKEN);
        if (tokens.length < 2) {
            log.error("Parse Rtsp response message error! first line:[{}]", line);
        }
        protocol = tokens[0];
        status = tokens[1];
    }


    public void addAuthPair(String k, String v) {
        this.authPairs.put(k,v);
    }

    public void addContent(String msgStr) {
        String[] contentLines = StringUtils.getRawLines(msgStr);
        int newSize = this.lines.length + contentLines.length;
        String[] newlines = new String[newSize];
        int i = 0;
        for (String line : this.lines) {
            newlines[i++] = line;
        }

        for (String line : contentLines) {
            newlines[i++] = line;
        }

        this.lines = newlines;
    }
}
